/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.maschinenstuermer.profiler.trace.provider;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITableItemLabelProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.emf.edit.provider.ViewerNotification;

import com.maschinenstuermer.profiler.trace.Metrics;
import com.maschinenstuermer.profiler.trace.TracePackage;

/**
 * This is the item provider adapter for a {@link com.maschinenstuermer.profiler.trace.Metrics} object.
 * <!-- begin-user-doc -->
 * @implements ITableItemLabelProvider  
 * <!-- end-user-doc -->
 * @generated
 */
public class MetricsItemProvider
	extends ItemProviderAdapter
	implements
		IEditingDomainItemProvider,
		IStructuredItemContentProvider,
		ITreeItemContentProvider,
		IItemLabelProvider,
		IItemPropertySource,
		ITableItemLabelProvider {
	/**
	 * This constructs an instance from a factory and a notifier.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public MetricsItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	/**
	 * This returns the property descriptors for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addCallsPropertyDescriptor(object);
			addMaxTimeNsPropertyDescriptor(object);
			addMinTimeNsPropertyDescriptor(object);
			addTotalTimeNsPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	/**
	 * This adds a property descriptor for the Calls feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void addCallsPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Metrics_calls_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Metrics_calls_feature", "_UI_Metrics_type"),
				 TracePackage.Literals.METRICS__CALLS,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Max Time Ns feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void addMaxTimeNsPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Metrics_maxTimeNs_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Metrics_maxTimeNs_feature", "_UI_Metrics_type"),
				 TracePackage.Literals.METRICS__MAX_TIME_NS,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Min Time Ns feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void addMinTimeNsPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Metrics_minTimeNs_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Metrics_minTimeNs_feature", "_UI_Metrics_type"),
				 TracePackage.Literals.METRICS__MIN_TIME_NS,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This adds a property descriptor for the Total Time Ns feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void addTotalTimeNsPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Metrics_totalTimeNs_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Metrics_totalTimeNs_feature", "_UI_Metrics_type"),
				 TracePackage.Literals.METRICS__TOTAL_TIME_NS,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This returns Metrics.gif.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/Metrics"));
	}

	/**
	 * This returns the label text for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String getText(Object object) {
		Metrics metrics = (Metrics)object;
		return getString("_UI_Metrics_type") + " " + metrics.getCalls();
	}

	/**
	 * This handles model notifications by calling {@link #updateChildren} to update any cached
	 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Metrics.class)) {
			case TracePackage.METRICS__CALLS:
			case TracePackage.METRICS__MAX_TIME_NS:
			case TracePackage.METRICS__MIN_TIME_NS:
			case TracePackage.METRICS__TOTAL_TIME_NS:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
		}
		super.notifyChanged(notification);
	}

	/**
	 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
	 * that can be created under this object.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);
	}

	/**
	 * Return the resource locator for this item provider's resources.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public ResourceLocator getResourceLocator() {
		return TraceEditPlugin.INSTANCE;
	}

	private double getRelativTime(final Metrics metrics) {
		final IRootInvocation adapter = (IRootInvocation) EcoreUtil.getRegisteredAdapter(metrics, IRootInvocation.class);
		return adapter.getRelativeTime(metrics);
	}

	@Override
	public String getColumnText(Object object, int columnIndex) {
		final Metrics metrics = (Metrics) object;
		final DecimalFormat percentFormat = (DecimalFormat) NumberFormat.getPercentInstance();
		percentFormat.setMaximumFractionDigits(0);
		percentFormat.setMinimumFractionDigits(0);
		percentFormat.setPositiveSuffix("");
		percentFormat.setNegativePrefix("");

		final DecimalFormat callsFormat = (DecimalFormat) NumberFormat.getIntegerInstance();
		callsFormat.setGroupingUsed(true);

		final DecimalFormat millisecondsFormat = (DecimalFormat) NumberFormat.getIntegerInstance();
		millisecondsFormat.setPositiveSuffix(" ms");
		millisecondsFormat.setGroupingUsed(true);
		
		switch (columnIndex) {
		case 0:
			return "Self Time";
		case 1:
			final double relativeTime = getRelativTime(metrics);
			return percentFormat.format(relativeTime);
		case 2:
			return millisecondsFormat.format(metrics.getTotalTimeNs() / 1000);
		case 3:
			return callsFormat.format(metrics.getCalls());
		default:
			return super.getColumnText(object, columnIndex);
		}
	}
	
	@Override
	public Object getColumnImage(Object object, int columnIndex) {
		if (columnIndex == 0) {
			return getImage(object);
		}
		else {
			return super.getColumnImage(object, columnIndex);
		}
	}
}
